﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

using LowLevelGraphics;
using LowLevelGraphics.Forms;

namespace ImageRecognition2.Action
{
    /// <summary>
    /// Used for delegates
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public delegate void ExecuteAction(object sender, EventArgs e);

    /// <summary>
    /// Executes a function given in a delegate
    /// </summary>
    public class ActionExecuteDelegate : AbstractCommand
    {
        protected Delegate m_Delegate = null;

        /// <summary>
        /// Default construcotr
        /// </summary>
        /// <param name="_delegate"></param>
        public ActionExecuteDelegate(Delegate _delegate)
        {
            m_Delegate = _delegate;
        }

        /// <summary>
        /// Execute delegate
        /// </summary>
        /// <param name="_executation"></param>
        public ActionExecuteDelegate(ExecuteAction _executation)
        {
            m_Delegate = _executation;
        }

        internal ActionExecuteDelegate(ActionExecuteDelegate _ActionExecuteDelegate)
        {
            m_Delegate = _ActionExecuteDelegate.m_Delegate;
        }

        /// <summary>
        /// Gets or sets the delegate.
        /// </summary>
        /// <value>The delegate.</value>
        public Delegate Delegate
        {
            get { return m_Delegate; }
            set { m_Delegate = value; }
        }

        /// <summary>
        /// Executes this action
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Run(object sender, EventArgs e)
        {
            Bitmap bitmap = (Bitmap)MainFormSingleton.ActivePictureBox.BackgroundImage;
            UnsafeBitmap unsafeBitmap = m_Delegate.DynamicInvoke(new object[] { new UnsafeBitmap(bitmap) }) as UnsafeBitmap;
            if (unsafeBitmap != null)
            {
                unsafeBitmap.Dispose();
            }
            else
            {
                Debug.WriteLine("UnsafeBitmap as return type expected, but was NULL");
            }
        }

        public override object Clone()
        {
            return new ActionExecuteDelegate(this);
        }
    }
}
